﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;
using System.Data;

namespace PeisongWeb.admin
{
    public partial class companyEdit : adminPage
    {
        public int companyId
        {
            get
            {
                int tmp = 0;
                int.TryParse(Request.QueryString["company"], out tmp);
                return tmp;
            }
            set
            {
                companyId = value;
            }
        }

        public int type
        {
            get
            {
                int tmp = 0;
                int.TryParse(Request.QueryString["type"], out tmp);
                return tmp;
            }
            set
            {
                type = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (hasPermission("FC", 1))
                {
                    sellerBindOptions();
                    buyerBindOptions();
                    CommonOptions();

                    if (companyId != 0 && type != 0)
                    {
                        switch (type)
                        {
                            case 1: companyTab.Tabs[0].Disabled = false; companyTab.Tabs[1].Disabled = true; companyTab.ActiveTabIndex = 0; break;
                            case 2: companyTab.Tabs[0].Disabled = true; companyTab.Tabs[1].Disabled = false; companyTab.ActiveTabIndex = 1; break;
                        }
                        getDetails();
                    }
                }
                else
                {
                    Library.Script.ClientMsgUrl("没有权限！", "index.aspx");
                    return;
                }
            }
        }

        private void sellerBindOptions()
        {
            ddlSellerType.DataSource = logic.sysCode.sellerTypeList();
            ddlSellerType.DataBind();

            ddlSellerCompanyClass.DataSource = logic.sysCode.sellerNatureList();
            ddlSellerCompanyClass.DataBind();

            rptSupplyclassMap.DataSource = logic.company.getCompanyBusinessNature(companyId);
            rptSupplyclassMap.DataBind();
        }

        private void buyerBindOptions()
        {
            rpt_buyerType.DataSource = logic.company.getBuyerTypeList(companyId);
            rpt_buyerType.DataBind();

            ddlBuyerNature.DataSource = logic.sysCode.buyerNatureList();
            ddlBuyerNature.DataBind();

            ddl_SalesPerson.DataSource = logic.sysAdmin.listWithSalesperson();
            ddl_SalesPerson.DataBind();
            ddl_SalesPerson.Items.Insert(0, new ListItem("--请选择销售员--", "0"));

            ddlBuyerReceiveTime.DataSource = logic.sysCode.receiveTime();
            ddlBuyerReceiveTime.DataBind();
            ddlBuyerReceiveTime.Items.Insert(0, new ListItem("--请选择送货时间--", ""));
        }

        private void CommonOptions()
        {
            // 地区
            DataTable areaDict = logic.sysCode.areaList();
            ddlBuyerArea.DataSource = areaDict;
            ddlBuyerArea.DataBind();
            ddlBuyerArea.Items.Insert(0, new ListItem("--请选择地区--", ""));

            ddlSellerArea.DataSource = areaDict;
            ddlSellerArea.DataBind();
            ddlSellerArea.Items.Insert(0, new ListItem("--请选择地区--", ""));

            // 结账方式
            DataTable payMethodDict = logic.sysCode.payMethodList();
            ddlBuyerPayMethod.DataSource = payMethodDict;
            ddlBuyerPayMethod.DataBind();
            ddlBuyerPayMethod.Items.Insert(0, new ListItem("--请选择结帐方式--", ""));

            ddlSellerPayMethod.DataSource = payMethodDict;
            ddlSellerPayMethod.DataBind();
            ddlSellerPayMethod.Items.Insert(0, new ListItem("--请选择结帐方式--", ""));
        }

        private void getDetails()
        {
            CompanyEntity companyEntity = logic.company.getById(companyId);
            if (type == 1)
            {
                #region
                txtSellerCompanyName.Text = companyEntity.Company;
                ddlSellerType.SelectedIndex = ddlSellerType.Items.IndexOf(ddlSellerType.Items.FindByValue(companyEntity.sellerType));
                ddlSellerCompanyClass.SelectedIndex = ddlSellerCompanyClass.Items.IndexOf(ddlSellerCompanyClass.Items.FindByValue(companyEntity.BusinessNature));
                txtSellerCompanyNo.Text = companyEntity.CompanyNo;
                ddlSellerArea.SelectedIndex = ddlSellerArea.Items.IndexOf(ddlSellerArea.Items.FindByValue(companyEntity.AreaID.ToString()));
                txtSellerAddress.Text = companyEntity.Address;
                txtSellerLocation.Text = companyEntity.Location;
                txtSellerPhone.Text = companyEntity.Phone;
                txtSellerFax.Text = companyEntity.Fax;
                txtSellerPostCode.Text = companyEntity.Postcode;
                txtSellerContact.Text = companyEntity.Contact;
                txtSellerMobile.Text = companyEntity.Mobile;
                txtSellerEmail.Text = companyEntity.Email;
                ddlSellerPayMethod.SelectedIndex = ddlSellerPayMethod.Items.IndexOf(ddlSellerPayMethod.Items.FindByValue(companyEntity.PayMethod));
                radFloat.Checked = (companyEntity.cooperateType == "1");
                radBranch.Checked = (companyEntity.cooperateType == "2");
                txtSellerCooperatePercent.Text = string.Format("{0:F2}", companyEntity.cooperatePercent);
                radFree.Checked=(companyEntity.QuotationMode=="1");
                radClosed.Checked = (companyEntity.QuotationMode == "2");
                chxSellerIsCheck.Checked = companyEntity.IsCheck;
                txtSellerSuggest.Text = companyEntity.Suggest;
                #endregion
            }
            else if (type == 2)
            {
                #region
                hdBuyerType.Value = companyEntity.buyerType;
                txtBuyerCompanyName.Text = companyEntity.Company;
                ddlBuyerNature.SelectedIndex = ddlBuyerNature.Items.IndexOf(ddlBuyerNature.Items.FindByValue(companyEntity.BusinessNature));
                ddl_SalesPerson.SelectedIndex = ddl_SalesPerson.Items.IndexOf(ddl_SalesPerson.Items.FindByValue(companyEntity.salesperson));
                txtBuyerCompanyNo.Text = companyEntity.CompanyNo;
                ddlBuyerArea.SelectedIndex = ddlBuyerArea.Items.IndexOf(ddlBuyerArea.Items.FindByValue(companyEntity.AreaID.ToString()));
                txtBuyerAddress.Text = companyEntity.Address;
                txtBuyerPhone.Text = companyEntity.Phone;
                txtBuyerFax.Text = companyEntity.Fax;
                txtBuyerPostCode.Text = companyEntity.Postcode;
                txtBuyerEatPerson.Text = companyEntity.EatPerson;
                txtBuyerContact.Text = companyEntity.Contact;
                txtBuyerIDCard.Text = companyEntity.IdCard;
                txtBuyerMobile.Text = companyEntity.Mobile;
                txtBuyerEmail.Text = companyEntity.Email;
                ddlBuyerPayMethod.SelectedIndex = ddlBuyerPayMethod.Items.IndexOf(ddlBuyerPayMethod.Items.FindByValue(companyEntity.PayMethod));
                txtBuyerPayDay.Text = companyEntity.AccountDay.ToString();
                ddlBuyerReceiveTime.SelectedIndex = ddlBuyerReceiveTime.Items.IndexOf(ddlBuyerReceiveTime.Items.FindByValue(companyEntity.ReceiveTime));
                chxBuyerIsCheck.Checked = companyEntity.IsCheck;
                txtBuyerSuggest.Text = companyEntity.Suggest;
                chkBuyerWeeklyPrice.Checked = companyEntity.EnabledWeeklyPrice;
                #endregion
            }
        }

        // 供应商
        protected void btn_Selller_Save_Click(object sender, EventArgs e)
        {
            if (companyId == 0)
            {
                if (logic.company.isExistCompanyNo(txtSellerCompanyNo.Text.Trim()))
                {
                    Library.Script.ClientMsg("企业编号已存在，请重新输入！");
                    return;
                }
            }

            #region
            CompanyEntity companyEntity = new CompanyEntity();
            companyEntity.Company = txtSellerCompanyName.Text;
            companyEntity.VipType = "1";
            companyEntity.Soruce = "1";
            companyEntity.Type = "1";
            companyEntity.AdminId = logic.sysAdmin.AdminID;
            companyEntity.sellerType = ddlSellerType.SelectedValue;
            companyEntity.BusinessNature = ddlSellerCompanyClass.SelectedValue;
            companyEntity.buyerType = "";
            companyEntity.CompanyNo = txtSellerCompanyNo.Text;
            companyEntity.AreaID = Convert.ToInt32(ddlSellerArea.SelectedValue);
            companyEntity.Address = txtSellerAddress.Text;
            companyEntity.Location = txtSellerLocation.Text;
            companyEntity.Phone = txtSellerPhone.Text;
            companyEntity.Fax = txtSellerFax.Text;
            companyEntity.Postcode = txtSellerPostCode.Text;
            companyEntity.Contact = txtSellerContact.Text;
            companyEntity.Mobile = txtSellerMobile.Text;
            companyEntity.Email = txtSellerEmail.Text;
            companyEntity.PayMethod = ddlSellerPayMethod.SelectedValue;
            if (radFloat.Checked)
            {
                companyEntity.cooperateType = "1";
            }
            else if (radBranch.Checked)
            {
                companyEntity.cooperateType = "2";
            }
            if (radFree.Checked)
            {
                companyEntity.QuotationMode = "1";
            }
            else if (radClosed.Checked)
            {
                companyEntity.QuotationMode = "2";
            }
            decimal sellerCooperatePercent = 0m;
            decimal.TryParse(txtSellerCooperatePercent.Text,out sellerCooperatePercent);
            companyEntity.cooperatePercent = sellerCooperatePercent;
            companyEntity.IsCheck = chxSellerIsCheck.Checked;
            companyEntity.Suggest = txtSellerSuggest.Text;

            int currentCompanyId = companyId;

            if (!string.IsNullOrEmpty(hdSellerBusinessNatures.Value))
            {
                if (companyId != 0 && type == 1)
                {
                    companyEntity.CompanyId = companyId;
                    logic.company.update(companyEntity);
                    logic.company.updateByCheck(companyEntity);
                }
                else
                {
                    currentCompanyId = logic.company.insert(companyEntity);
                    companyEntity.CompanyId = currentCompanyId;
                    logic.company.updateByCheck(companyEntity);
                }

                logic.company.saveCompanyBusniessNature(logic.sysAdmin.AdminID, currentCompanyId, hdSellerBusinessNatures.Value);

                Library.Script.ClientMsgUrl("保存成功！", "companyList.aspx");
            }
            #endregion
        }

        // 采购商
        protected void btn_Buyer_Save_Click(object sender, EventArgs e)
        {
            if (companyId == 0)
            {
                if (logic.company.isExistCompanyNo(txtBuyerCompanyNo.Text.Trim()))
                {
                    Library.Script.ClientMsg("企业编号已存在，请重新输入！");
                    return;
                }
            }

            #region
            CompanyEntity companyEntity = new CompanyEntity();
            companyEntity.VipType = "1";
            companyEntity.Soruce = "1";
            companyEntity.Type = "2";
            companyEntity.buyerType = hdBuyerType.Value;
            companyEntity.AdminId = logic.sysAdmin.AdminID;
            companyEntity.Company = txtBuyerCompanyName.Text;
            companyEntity.BusinessNature = ddlBuyerNature.SelectedValue;
            companyEntity.salesperson = ddl_SalesPerson.SelectedValue;
            companyEntity.CompanyNo = txtBuyerCompanyNo.Text;
            companyEntity.AreaID = Convert.ToInt32(ddlBuyerArea.SelectedValue);
            companyEntity.Address = txtBuyerAddress.Text;
            companyEntity.Phone = txtBuyerPhone.Text;
            companyEntity.Fax = txtBuyerFax.Text;
            companyEntity.Postcode = txtBuyerPostCode.Text;
            companyEntity.EatPerson = txtBuyerEatPerson.Text;
            companyEntity.Contact = txtBuyerContact.Text;
            companyEntity.IdCard = txtBuyerIDCard.Text;
            companyEntity.Mobile = txtBuyerMobile.Text;
            companyEntity.Email = txtBuyerEmail.Text;
            companyEntity.PayMethod = ddlBuyerPayMethod.SelectedValue;
            int accountDay = 0;
            int.TryParse(txtBuyerPayDay.Text, out accountDay);
            companyEntity.AccountDay = accountDay;
            companyEntity.ReceiveTime = ddlBuyerReceiveTime.Text;
            companyEntity.IsCheck = chxBuyerIsCheck.Checked;
            companyEntity.Suggest = txtBuyerSuggest.Text;
            companyEntity.EnabledWeeklyPrice = chkBuyerWeeklyPrice.Checked;
            companyEntity.QuotationMode = "";

            int currentCompanyId = companyId;

            if (companyId != 0 && type == 2)
            {
                string businessNatures = logic.company.getById(companyId).BusinessNature;//修改前保存原有的企业性质
                companyEntity.CompanyId = companyId;
                logic.company.update(companyEntity);
                logic.company.updateByCheck(companyEntity);

                if (companyEntity.BusinessNature != businessNatures)//判断该企业性质是否改变
                {
                    logic.buyerRange.buyerRangeBycompanyIdBusinessNature(companyId, companyEntity.BusinessNature, businessNatures);
                    Library.Script.ClientMsgUrl("原有采购范围中的商品已与企业性质同步！", "companyList.aspx");
                }
                else
                {
                    //初始化系统加价体系，针对系统中所有在销售商品
                    if (companyEntity.IsCheck)//已通过审核
                    {
                        try
                        {
                            logic.priceStructure.InsertProductAutoAdd(companyEntity.BusinessNature, companyEntity.CompanyId, companyEntity.AdminId);
                        }
                        catch { }
                    }
                }
                Library.Script.ClientMsgUrl("保存成功！", "companyList.aspx");
            }
            else
            {
                currentCompanyId = logic.company.insert(companyEntity);
                companyEntity.CompanyId = currentCompanyId;
                logic.company.updateByCheck(companyEntity);

                //初始化系统加价体系，针对系统中所有在销售商品
                if (companyEntity.IsCheck)//已通过审核
                {
                    try
                    {
                        logic.priceStructure.InsertProductAutoAdd(companyEntity.BusinessNature, companyEntity.CompanyId, companyEntity.AdminId);
                    }
                    catch { }
                }
                Library.Script.ClientMsgUrl("保存成功！", "companyList.aspx");
            }
            #endregion
        }
    }
}
